/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.fyx.mula.ejb;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import org.fyx.mula.controll.MulaTranslateFacadeLocal;
import org.fyx.mula.entity.MulaTranslate;
import org.fyx.mula.entity.MulaTranslatePK;
import org.fyx.secure.ejb.FyxContext;
import org.fyx.core.context.FyxContextFactory;

/**
 *
 * @author ydenek
 */
@Stateless
public class MulaBean implements MulaLocal {

    @EJB
    private MulaTranslateFacadeLocal mulaTranslateFacade;

    @Override
    public String get(String key) {
        FyxContext ctx = FyxContextFactory.getContext();
        return get(key, ctx.getIsoLanguage());
    }

    @Override
    public String get(String key, String isoLanguage) {
        if (key == null || key.equals("")) {
            return "";
        }
        MulaTranslatePK pk = new MulaTranslatePK(key, isoLanguage);
        MulaTranslate mulaTranslate = mulaTranslateFacade.find(pk);
        if (mulaTranslate == null) {
            mulaTranslate = new MulaTranslate(pk);
            mulaTranslate.setTranslate("");
            mulaTranslateFacade.create(mulaTranslate);
            return key;
        }
        if (mulaTranslate.getTranslate().equals("")) {
            return key;
        }
        return mulaTranslate.getTranslate();
    }

    @Override
    public void setTranslate(String key, String transtate, String isoLanguage){
        MulaTranslatePK pk = new MulaTranslatePK(key, isoLanguage);
        MulaTranslate mulaTranslate = mulaTranslateFacade.find(pk);

        if (mulaTranslate == null) {
            mulaTranslate = new MulaTranslate(pk);
            mulaTranslate.setTranslate(transtate);
            mulaTranslateFacade.create(mulaTranslate);
        } else {
            mulaTranslate.setTranslate(transtate);
            mulaTranslateFacade.edit(mulaTranslate);
        }
    }

    @Override
    public void removeTranslate(String key, String isoLanguage) {
        mulaTranslateFacade.remove(new MulaTranslate(key, isoLanguage));
    }
}
